<script lang="ts" setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref, reactive } from 'vue'
import { articleCategoryList, articleCategoryAdd, articleCategoryUpdate, articleCategoryDelete } from '@/api/artice.js'
import { ElMessage, ElMessageBox, FormInstance, FormRules } from 'element-plus'  // 添加这行引入
const categorys = ref([])
const title = ref('')
const ruleFormRef = ref<FormInstance>()
const pageNum = ref(1)
const pageSize = ref(10)
const request = ref({
    pageNum: pageNum,
    pageSize: pageSize
})
const background = ref(false)
const disabled = ref(false)
const total = ref()
// 重置表单数据，及校验规则
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    formEl.clearValidate() // 清除验证状态
}
const handleSizeChange = (number) => {
    pageSize.value = number;
    getCategoryList()
}
const handleCurrentChange = (number) => {
    pageNum.value = number;
    getCategoryList()
}
// 获取文章分类列表
const getCategoryList = async () => {
    let res = await articleCategoryList(request.value);
    categorys.value = res.data.items;
    total.value = res.data.total;
}
// 调用方法
getCategoryList();

// 添加分类
const addCategory = async () => {
    let res = await articleCategoryAdd(categoryModel.value);
    if (res.code === 0) {
        ElMessage.success('添加成功');
        // 关闭弹窗
        dialogVisible.value = false;
        // 刷新列表
        getCategoryList();
    }
}
const updateCategory = async () => {
    let res = await articleCategoryUpdate(categoryModel.value);
    if (res.code === 0) {
        ElMessage.success(res.message ? res.message : '修改成功');
        // 关闭弹窗
        dialogVisible.value = false;
        // 刷新列表
        getCategoryList();
    }
}



const showDialog = (row) => {
    dialogVisible.value = true;
    title.value = '编辑分类';
    // 数据拷贝
    categoryModel.value.categoryAlias = row.categoryAlias;
    categoryModel.value.categoryName = row.categoryName;
    categoryModel.value.id = row.id;



}

const showDelete = (row: { id: any }) => {

    ElMessageBox.confirm(
        '是否要删除此分类',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            let res = await articleCategoryDelete(row.id);
            ElMessage({
                type: 'success',
                message: '删除成功',
            })
            getCategoryList();
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}

//控制添加分类弹窗
const dialogVisible = ref(false)
const showAdd = () => {
    dialogVisible.value = true;
    title.value = '添加分类';
    // 数据清空   
}
//添加分类数据模型
const categoryModel = ref({
    id: '',
    categoryName: '',
    categoryAlias: ''
})
//添加分类表单校验
const rules = reactive<FormRules<typeof categoryModel.value>>({
    categoryName: [
        { required: true, message: '请输入分类名称', trigger: 'blur' },
    ],
    categoryAlias: [
        { required: true, message: '请输入分类别名', trigger: 'blur' },
    ]
})

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>文章分类</span>
                <div class="extra">
                    <el-button type="primary" @click="showAdd()">添加分类</el-button>
                </div>
            </div>
        </template>
        <div class="table-container">
            <el-table :data="categorys" style="width: 100%">
                <!-- 表格列定义 -->
                <el-table-column label="序号" width="100" type="index"> </el-table-column>
                <el-table-column label="分类名称" prop="categoryName"></el-table-column>
                <el-table-column label="分类别名" prop="categoryAlias"></el-table-column>
                <el-table-column label="操作" width="100">
                    <template #default="{ row }">
                        <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                        <el-button :icon="Delete" circle plain type="danger" @click="showDelete(row)"></el-button>
                    </template>
                </el-table-column>
                <template #empty>
                    <el-empty description="没有数据" />
                </template>
            </el-table>

            <div class="pagination-wrapper">
                <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize"
                    :page-sizes="[10, 20, 50, 100]" :disabled="disabled" :background="background"
                    layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
                    @current-change="handleCurrentChange" hide-on-single-page="true" />
            </div>
        </div>
        <!--  添加分类 -->
        <el-dialog v-model="dialogVisible" :title="title" width="30%" @close="resetForm(ruleFormRef)">
            <el-form ref="ruleFormRef" :model="categoryModel" :rules="rules" label-width="100px"
                style="padding-right: 30px">
                <el-form-item label="分类名称" prop="categoryName">
                    <el-input v-model="categoryModel.categoryName" minlength="1" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item label="分类别名" prop="categoryAlias">
                    <el-input v-model="categoryModel.categoryAlias" minlength="1" maxlength="15"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false; resetForm(ruleFormRef)">取消</el-button>
                    <el-button type="primary" @click="title === '添加分类' ? addCategory() : updateCategory()"> 确认
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .table-container {
        display: flex;
        flex-direction: column;
        height: 100%;
        position: relative;

        .pagination-wrapper {
            margin-top: auto;
            padding: 20px 0;
            display: flex;
            justify-content: end;
        }
    }
}
</style>